var app = getApp()
// pages/order_list/order_list.js
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    selected: false,
    selected1: false,
    selected2: false,
    selected3: false,
    selected4: false,
    status: '',
    order_list: [],
    firstRow: 0,
    k: '',
    u: '',
    request_num: '',//加载底部使用
    show: 0,
    is_show_time: 1,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var status = options.state
    var firstRow = that.data.firstRow
    that.setData({
      k: key,
      u: uid,
      status: status
    })
    switch (status) {
      case 'all':
        that.setData({
          selected: true
        })
        break;
      case 'chain_finish':
        that.setData({
          selected1: true
        })
        break;
      case 'order_payed':
        that.setData({
          selected2: true
        })
        break;
      case 'order_chain':
        that.setData({
          selected3: true
        })
        break;
      case 'wait_pay':
        that.setData({
          selected4: true
        })
        break;
      default:
        that.setData({
          selected: true
        })
        break;
    }
    if (key) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=chain&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: key, u: uid, status: status },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          that.setData({
            order_list: res.data.data.items,
            is_show_time: 0
          })
        }
      })
    } else {
      that.setData({
        is_show_time: 0
      })
    }
  },

  onShow: function () {
    var that = this;
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var status = that.data.status
    var firstRow = that.data.firstRow
    that.setData({
      k: key,
      u: uid,
      status: status
    })
    switch (status) {
      case 'all':
        that.setData({
          selected: true
        })
        break;
      case 'chain_finish':
        that.setData({
          selected1: true
        })
        break;
      case 'received':
        that.setData({
          selected2: true
        })
        break;
      case 'order_chain':
        that.setData({
          selected3: true
        })
        break;
      case 'wait_pay':
        that.setData({
          selected4: true
        })
        break;
      default:
        that.setData({
          selected: true
        })
        break;
    }
    if (key) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=chain&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: key, u: uid, status: status },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          that.setData({
            order_list: res.data.data.items,
            is_show_time: 0
          })
        }
      })
    } else {
      that.setData({
        is_show_time: 0
      })
    }
  },
  //订单列表切换
  order: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.status
    var firstRow = 0
    switch (status) {
      case 'all':
        that.setData({
          selected: true,
          selected1: false,
          selected2: false,
          selected3: false,
          selected4: false,
        })
        break;
      case 'chain_finish':
        that.setData({
          selected: false,
          selected1: true,
          selected2: false,
          selected3: false,
          selected4: false,

        })
        break;
      case 'received':
        that.setData({
          selected: false,
          selected1: false,
          selected2: true,
          selected3: false,
          selected4: false,
        })
        break;
      case 'order_chain':
        that.setData({
          selected: false,
          selected1: false,
          selected2: false,
          selected3: true,
          selected4: false,
        })
        break;
      case 'wait_pay':
        that.setData({
          selected: false,
          selected1: false,
          selected2: false,
          selected3: false,
          selected4: true,
        })
        break;
    }
    if (that.data.k) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=chain&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          that.setData({
            order_list: res.data.data.items,
            status: status,
            firstRow: firstRow,
            request_num: '',
            is_show_time: 0
          })

        }
      })
    }
  },


  //删除订单
  delete_order: function (e) {
    var order_id = e.target.dataset.order_id
    this.setData({
      del_nocancel: true,
      order_id: order_id
    })
  },
  del_confirm: function () {
    var can_id = this.data.order_id
    this.delete_orderss(can_id)
  },
  del_cancel: function () {
    this.setData({
      del_nocancel: false,
    })
  },
  //删除订单
  delete_orderss: function (can_id) {
    var that = this
    var order_id = can_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=hideOrder&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.status == 200) {
          app.show('成功', 'success')
          that.onShow()
        } else {
          app.show('删除失败', 'loading')
        }
        that.setData({
          del_nocancel: false,
        })
      }, fail: function (res) {
        app.show('数据加载失败', 'loading')
      }
    })

  },
  //确认订单
  sure_order: function (e) {
    var order_id = e.target.dataset.order_id
    this.setData({
      con_nocancel: true,
      order_id: order_id
    })
  },
  con_confirm: function () {
    var con_id = this.data.order_id
    this.sure_orderss(con_id)
  },
  con_cancel: function () {
    this.setData({
      con_nocancel: false,
    })
  },

  //确认订单
  sure_orderss: function (con_id) {
    var that = this
    var order_id = con_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=confirmOrder&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.status == 200) {
          app.show('收货完成', 'success')
          that.onShow()
        } else {
          app.show('收货失败', 'loading')
        }
        that.setData({
          con_nocancel: false,
        })
      }, fail: function (res) {
        app.show('数据加载失败', 'loading')
      }
    })

  },
  //取消订单
  cancel_order: function (e) {
    var order_id = e.target.dataset.order_id
    this.setData({
      nocancel: true,
      order_id: order_id
    })
  },
  confirm: function () {
    var del_id = this.data.order_id
    this.cancel_orderss(del_id)
  },
  cancel: function () {
    this.setData({
      nocancel: false,
    })
  },
  //取消订单
  cancel_orderss: function (del_id) {
    var that = this
    var order_id = del_id
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=orderCancel&typ=json',
      method: 'GET',
      data: { k: that.data.k, u: that.data.u, order_id: order_id, user: 'buyer' },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        if (res.data.status == 200) {
          app.show('取消成功', 'success')
          that.onShow()
        } else {
          app.show('取消失败', 'loading')
        }
        that.setData({
          nocancel: false,
        })
      }, fail: function (res) {
        app.show('数据加载失败', 'loading')
      }
    })

  },

  //订单支付
  payOrder: function (e) {
    var that = this
    var uorder_id = e.currentTarget.dataset.payment_number
    var openid = wx.getStorageSync('openid');

    wx.request({
      url: app.configData.paycenter.api_url + '/index.php?ctl=Pay&met=createWXOrder&typ=json',
      method: 'GET',
      data: {
        k: that.data.k,
        u: that.data.u,
        uorder_id: uorder_id,
        openid: openid,
        trade_type: 'WXAPP'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {

        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': 'MD5',
          'paySign': res.data.paySign,
          'success': function (res) {
            app.show('支付成功', 'success');
            wx.request({
              url: app.configData.paycenter.api_url + '/index.php?ctl=Pay&met=order_status&typ=json',
              method: 'GET',
              data: {
                k: that.data.k,
                u: that.data.u,
                order_id: uorder_id,
                buyer_id: that.data.u,
              },
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                wx.redirectTo({
                  url: '../order_list/order_list'
                })

              }


            })
          },
          'fail': function (res) {

          },
          'complete': function (res) {

          }
        });
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.k) {
      var firstRow = that.data.firstRow + 10
      var status = that.data.status

      if (that.data.request_num == 1) {
        return false
      }

      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Buyer_Order&met=chain&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.data.items.length > 0) {
            var order_list = that.data.order_list
            for (var z in res.data.data.items) {
              order_list.push(res.data.data.items[z])
            }
            that.setData({
              order_list: order_list,
              firstRow: firstRow,
              is_show_time: 0
            })
          } else {
            that.setData({
              request_num: 1,
              is_show_time: 0
            })
          }
        }
      })
    }
  },
  bindhref: function (e) {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {

        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },


})